package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.framework.exception.ServiceException;
import com.mars.module.admin.entity.*;
import com.mars.module.admin.mapper.SysScenicSpotMapper;
import com.mars.module.admin.mapper.SysTravelRouteDetailsMapper;
import com.mars.module.admin.mapper.SysTravelRouteMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysTravelRouteDetailsRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysTravelRouteDetailsMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysTravelRouteDetails;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysTravelRouteDetailsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 旅游路线详情业务层处理
 *
 * @author mars
 * @date 2024-03-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysTravelRouteDetailsServiceImpl implements ISysTravelRouteDetailsService {

    private final SysTravelRouteDetailsMapper baseMapper;
    private final SysTravelRouteMapper routeMapper;
    private final SysScenicSpotMapper spotMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysTravelRouteDetails add(SysTravelRouteDetailsRequest request) {
        Long l = baseMapper.selectCount(new QueryWrapper<SysTravelRouteDetails>().eq("route_id", request.getRouteId()).eq("spot_id", request.getSpotId()));
        if (l > 0) {
            throw new ServiceException("当前路线已存在该景点！");
        }
        SysScenicSpot scenicSpot = spotMapper.selectById(request.getSpotId());
        SysTravelRoute route = routeMapper.selectById(request.getRouteId());
        if (route != null && scenicSpot != null) {
            if (scenicSpot.getAreaId() != route.getAreaId()) {
                throw new ServiceException("当前选择景点不属于路线对应的景区！");
            }
        }
        SysTravelRouteDetails entity = SysTravelRouteDetails.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysTravelRouteDetailsRequest request) {
        SysTravelRouteDetails entity = SysTravelRouteDetails.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysTravelRouteDetails getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysTravelRouteDetails> pageList(SysTravelRouteDetailsRequest request) {
        Page<SysTravelRouteDetails> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysTravelRouteDetails> query = this.buildWrapper(request);
        IPage<SysTravelRouteDetails> pageRecord = baseMapper.selectPage(page, query);
        List<SysTravelRouteDetails> records = pageRecord.getRecords();
        List<SysTravelRouteDetails> recordsNew = new ArrayList<>();
        for (SysTravelRouteDetails e : records) {
            SysTravelRoute route = routeMapper.selectById(e.getRouteId());
            SysScenicSpot scenicSpot = spotMapper.selectById(e.getSpotId());
            e.setRouteName(route == null ? "" : route.getName());
            e.setSpotName(scenicSpot == null ? "" : scenicSpot.getName());
            recordsNew.add(e);
        }
        if (StringUtils.isNotBlank(request.getRouteName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getRouteName().contains(request.getRouteName())).collect(Collectors.toList());
        }
        if (StringUtils.isNotBlank(request.getSpotName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getSpotName().contains(request.getSpotName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysTravelRouteDetails> list(SysTravelRouteDetailsRequest request) {
        LambdaQueryWrapper<SysTravelRouteDetails> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysTravelRouteDetails> buildWrapper(SysTravelRouteDetailsRequest param) {
        LambdaQueryWrapper<SysTravelRouteDetails> query = new LambdaQueryWrapper<>();
        if (param.getSortNo() != null) {
            query.eq(SysTravelRouteDetails::getSortNo, param.getSortNo());
        }
        if (param.getSpotId() != null) {
            query.eq(SysTravelRouteDetails::getSpotId, param.getSpotId());
        }
        return query;
    }

}
